/**
 * AddCircleBillActivity.java
 * com.jamin.aabill.ui
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-30 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.jamin.aabill.R;
import com.jamin.aabill.custom.TopBar;
import com.jamin.aabill.custom.TopBarClickListener;
import com.jamin.aabill.custom.dialog.AddBillMemberDialog;
import com.jamin.aabill.custom.dialog.AddBillMemberDialog.AddBillMemberDialogLisener;
import com.jamin.aabill.data.BillViewDetail;
import com.jamin.aabill.utils.TimeUtils;
import com.jamin.aabill.utils.Utils;

/**
 * ClassName:AddCircleBillActivity
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-30 上午9:06:51 
 */
public class AddCircleBillActivity extends Activity implements OnClickListener{

	private static final String TAG = "AddCircleBillActivity============wangjieming";
	private static final int FLAG_PAY = 0;
	private static final int FLAG_PROFIT = 1;

	ArrayList<String> mPayList = new ArrayList<String>();
	ArrayList<String> mProfitList  = new ArrayList<String>();
	long circleId = -1;
	int type = -1;
	
	BillViewDetail billViewDetail = null;


	TopBar mTopBar = null;					//topBar
	DatePicker mDatePicker = null;			//时间选择器
	EditText mProjectEditText = null;		//活动名称
	EditText mMoneyEditText = null;			//金额
	Spinner mTypeSpinner = null;			//选择消费类型
	Button mAddPayMemberBtn = null;			//增加付款人按钮
	TextView mPayMemberTextView = null;		//付款人姓名
	Button mAddProfitMemberBtn = null;		//增加受益人按钮
	TextView mProfitMemberTextView =null;	//受益人姓名
	Dialog mAddBillMemberDialog = null;		//增加Member的Dialog(包括付款人和受益人)
	LinearLayout mPayMemberLayout = null;
	Button mCommitBillBtn = null;			//确定增加订单按钮

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_circle_bill_activity);
		circleId = getIntent().getLongExtra("circleId", -1);
		initView();


	}



	private void initView() {
		mTopBar = (TopBar) findViewById(R.id.top_bar);
		mTopBar.setTopBarClickListener(new TopBarClickListener() {
			@Override
			public void rightBtnClick() {
				Intent intent = new Intent(AddCircleBillActivity.this , AddCircleMemberActivity.class);
				intent.putExtra("circleId", circleId);
				startActivity(intent);
			}
			@Override
			public void right2BtnClick() {
			}
			@Override
			public void leftBtnClick() {
				finish();
			}
		});
		mDatePicker = (DatePicker) findViewById(R.id.add_bill_activity_time_picker);
		mAddPayMemberBtn = (Button) findViewById(R.id.add_bill_pay_person_title_btn);
		mAddProfitMemberBtn = (Button) findViewById(R.id.add_bill_profit_person_title_btn);
		mPayMemberTextView = (TextView) findViewById(R.id.add_bill_pay_member_name_text);
		mProfitMemberTextView = (TextView) findViewById(R.id.add_bill_profit_member_name_text);
		mProjectEditText = (EditText) findViewById(R.id.add_bill_activity_name_edit);
		mMoneyEditText = (EditText) findViewById(R.id.add_bill_money_edit);
		mTypeSpinner = (Spinner) findViewById(R.id.add_bill_type_spinner);
		mTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view,
					int position, long arg3) {
				type = getResources().getIntArray(R.array.bill_type_id)[position];
				Utils.log(TAG, "pay type = " + type);
			}
			@Override
			public void onNothingSelected(AdapterView<?> adapterView) {
			}
		});
		mPayMemberLayout = (LinearLayout) findViewById(R.id.add_bill_pay_member_layout);
		mCommitBillBtn = (Button) findViewById(R.id.add_bill_ok_btn);
		mAddPayMemberBtn.setOnClickListener(this);
		mAddProfitMemberBtn.setOnClickListener(this);
		mCommitBillBtn.setOnClickListener(this);

	}




	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.add_bill_pay_person_title_btn:
			showAddBillMemberDialog(FLAG_PAY);
			break;
		case R.id.add_bill_profit_person_title_btn:
			showAddBillMemberDialog(FLAG_PROFIT);
			break;
		case R.id.add_bill_ok_btn:
			commitBill();
			break;
		}

	}


	private void commitBill() {
		Animation shake = AnimationUtils.loadAnimation(AddCircleBillActivity.this, R.anim.shake);
		if(TextUtils.isEmpty(mProjectEditText.getText().toString())){
			mProjectEditText.setAnimation(shake);
		}else if(TextUtils.isEmpty(mMoneyEditText.getText().toString())){
			mMoneyEditText.setAnimation(shake);
		}else if(mPayList.size() == 0){
			Toast.makeText(this, getString(R.string.toast_bill_no_pay_member), Toast.LENGTH_SHORT).show();
		}else if(mProfitList.size() == 0){
			Toast.makeText(this,  getString(R.string.toast_bill_no_profit_member), Toast.LENGTH_SHORT).show();
		}else{
			long time = TimeUtils.FormatTimeFromDatePicker(mDatePicker.getYear(), mDatePicker.getMonth() + 1 , mDatePicker.getDayOfMonth());
			String project = mProjectEditText.getText().toString();
			double money = Double.parseDouble(mMoneyEditText.getText().toString());
			AABillApplication.mBillManager.saveEqualBillList(time , project, circleId, type, money, mPayList, mProfitList);
			finish();
		}
	}



	private void showAddBillMemberDialog(final int flag){
		mAddBillMemberDialog = new AddBillMemberDialog(this, circleId ,  new AddBillMemberDialogLisener() {
			@Override
			public void addMemberView(ArrayList<String> memberNameList,ArrayList<String> memberIdList) {
				String str = "";
				for(int i = 0 ; i < memberNameList.size() ;i++ ){
					str += memberNameList.get(i);
					if(i != memberNameList.size() - 1){
						str += "\n";
					}
				}
				if(flag == FLAG_PAY){
					mPayList = memberIdList;
					mPayMemberTextView.setText(str);
				}else{
					mProfitList = memberIdList;
					mProfitMemberTextView.setText(str);
				}
			}
		});
		mAddBillMemberDialog.show();
	}




}

